#include "sleeexcelbook.h"



SleeExcelBook::SleeExcelBook(const SleeQAxPtr &exbook, quint32 id):
    m_exbook(exbook),
    m_id(id),
    m_last_id(0)
{
    m_sheets.mp = m_exbook.mp->querySubObject("Sheets");
}

void SleeExcelBook::close(bool do_save)
{
    foreach(SleeExcelSheet *p_sheet, mqh_sheets)
        p_sheet->free_ptr();
    m_sheets.free_ptr();
    if(!do_save)
        m_exbook.mp->dynamicCall("Close(const QVariant &)", QVariant(0));
    m_exbook.free_ptr();
}

SleeExcelSheet *SleeExcelBook::get_sheet(QVariant &qv_sheet)
{
    SleeQAxPtr sheet;
    sheet.mp = m_sheets.mp->querySubObject("Item(QVariant &)", qv_sheet);
    if(!sheet.is_ok()) return NULL;
    quint32 id = gen_id();
    mqh_sheets[id] = new SleeExcelSheet(sheet,id);
    return mqh_sheets[id];
}


SleeExcelSheet *SleeExcelBook::get_sheet(const QString &name)
{
    QVariant qv = name;
    return get_sheet(qv);
}

SleeExcelSheet *SleeExcelBook::get_sheet(quint32 index)
{
    QVariant qv = index;
    return get_sheet(qv);
}

void SleeExcelBook::save()
{
    m_exbook.mp->dynamicCall("Save()");
}

void SleeExcelBook::save_as(const QString &qstr_file_pathname)
{
    QVariant qv_file = qstr_file_pathname;
    m_exbook.mp->dynamicCall("SaveAs(QVariant)", qv_file);
}
